package com.sinosoft.msf.base.supplier.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;

import com.sinosoft.msf.base.supplier.pojo.FileUploadPojo;
import com.sinosoft.msf.base.supplier.pojo.RestEntity;
import com.sinosoft.msf.base.supplier.service.CommonInterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/base")
public class BaseshareController {
    @Autowired
    CommonInterService commonInterService;

    /**
     * 加密分享链接
     *
     * @return
     */
    @RequestMapping("/share/creatUrl")
    public RestEntity uploadFile() {
        String id = commonInterService.creatProjectId();
        String url = commonInterService.fetchComInputUrl(id);
        return RestEntity.data(url);
    }
  /**
     * 加密分享链接
     *
     * @return
     */
    @RequestMapping("/creatInputId")
    public RestEntity creatInputId() {
        return RestEntity.data(commonInterService.creatProjectId());
    }


    /**
     * 上传文件接口
     *
     * @param file
     * @param json
     * @return
     */
    @RequestMapping("/upload")
    public RestEntity uploadFile(@RequestParam MultipartFile file, String json) {
        FileUploadPojo fileUploadPojo = new FileUploadPojo();
        if(StrUtil.isNotEmpty(json)){
            fileUploadPojo  = JSONObject.parseObject(json, FileUploadPojo.class);
        }
        fileUploadPojo.setFile(file);
        return RestEntity.data(commonInterService.uploadFile(fileUploadPojo));
    }

    @RequestMapping("/dict/holdScope")
    public RestEntity dictHoldScope() {
        return RestEntity.data(commonInterService.supDict());
    }

    /**
     * 获取供应商免登陆链接
     */
    @RequestMapping("/share/getUrl")
    public RestEntity getCompanyUrl(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        Validator.validateNotEmpty(id, "供应商id不可为空");
        String url = commonInterService.fetchComInputUrl(id);
        return RestEntity.data(url);
    }
}